package arithmetic.socket.netty.chat;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

/**
 * netty做的聊天室
 * @author jiangfeng
 * Created on 2020-02-04
 */
public class ChatServer {

    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup boss = new NioEventLoopGroup();
        EventLoopGroup worker = new NioEventLoopGroup();
        try {
            //Bootstrap bootstrap = new Bootstrap();
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            // handler 作用于boss group, childHeadler作用于worker group
            serverBootstrap.group(boss, worker).channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.WARN))
                    .childHandler(new ChatServerInitializer());
            ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
            channelFuture.channel().closeFuture().sync();

        } finally {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }

    }

    // EventLoopGroup bossGroup = new NioEventLoopGroup();
    //        EventLoopGroup workerGroup = new NioEventLoopGroup();
    //
    //        try {
    //            ServerBootstrap serverBootstrap = new ServerBootstrap();
    //            serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).
    //                    handler(new LoggingHandler(LogLevel.WARN)).
    //                    childHandler(new MyServerInitializer());
    //
    //            ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
    //            channelFuture.channel().closeFuture().sync();
    //        } finally {
    //            bossGroup.shutdownGracefully();
    //            workerGroup.shutdownGracefully();
    //        }
}
